﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Apache.NMS;
using Apache.NMS.Util;
using Common;
using SuppliersUnitTest.Properties;

namespace SuppliersUnitTest
{
    public class Tools
    {
        private static IConnectionFactory factory;

        //factory = new NMSConnectionFactory(Constants.QUEUE_URI);

        public static void QueueCleanup(string queue)
        {
            if (factory == null)
                factory = new NMSConnectionFactory(Constants.QUEUE_URI);

            using (IConnection connection = factory.CreateConnection())
            using (ISession session = connection.CreateSession())
            {
                IDestination destination = SessionUtil.GetDestination(session, queue);
                // Create a consumer
                using (IMessageConsumer consumer = session.CreateConsumer(destination))
                {
                    connection.Start();
                    do
                    {
                    } while (consumer.Receive(new TimeSpan(0, 0, 0, 1)) != null);
                }
            }
        }

        public static void QueueSend(string sendQueueName, string message)
        {
            if (factory == null)
                factory = new NMSConnectionFactory(Constants.QUEUE_URI);


            using (IConnection connection = factory.CreateConnection())
            using (ISession session = connection.CreateSession())
            {
                IDestination destination = SessionUtil.GetDestination(session, sendQueueName);
                using (IMessageProducer producer = session.CreateProducer(destination))
                {
                    producer.DeliveryMode = MsgDeliveryMode.Persistent;
                    ITextMessage request = session.CreateTextMessage(message);
                    producer.Send(request);
                }
            }
        }


        public static IMessage GetQueueMessage(string queue)
        {
            if (factory == null)
                factory = new NMSConnectionFactory(Constants.QUEUE_URI);

            IMessage res;
            using (IConnection connection = factory.CreateConnection())
            using (ISession session = connection.CreateSession())
            {
                IDestination destination = SessionUtil.GetDestination(session, queue);
                // Create a consumer
                using (IMessageConsumer consumer = session.CreateConsumer(destination))
                {
                    connection.Start();
                    res = consumer.Receive(new TimeSpan(0, 0, 0, 5));
                }
            }
            return res;
        }



        public static string GetTestXml(string resourceName)
        {
            var xml = Resources.ResourceManager.GetObject(resourceName);
            return xml.ToString();
        }



        public static XDocument LoadMyXml(string xmlString)
        {
            XDocument doc = null;
            try
            {
                //Validate XML velformed
                doc = XDocument.Parse(xmlString);
            }
            catch (Exception e)
            {

            }
            return doc;
        }
    }
}